package com.example.controller;

import com.example.pojo.entity.JobRecruiter;
import com.example.pojo.entity.JobSeeker;
import com.example.service.JobSeekerService;
import com.example.service.RecruiterService;
import com.example.util.JWTUtils;
import com.example.vo.Result;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author 小岁
 * @version 2023/10/6 10:38
 */
@RestController
@RequestMapping("RecruiterLogin")
public class RecruiterController {
    @Autowired
    private RecruiterService recruiterService;

    private static final String SECRET = "success";//盐值
    private static String SECRET_KEY;
    ;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("jrLogin/{jrAccount}/{jrCipher}")
    public Result login(@PathVariable("jrAccount") String jrAccount, @PathVariable("jrCipher") String jrCipher) {
        JobRecruiter jobRecruiter = recruiterService.login(jrAccount);
        ValueOperations vop = redisTemplate.opsForValue();
        if (jobRecruiter == null) {
            return new Result(404, "用户名不存在");
        }
        //校验
        jrCipher = Jwts.builder().claim("password",jrCipher).signWith(SignatureAlgorithm.HS256,SECRET).compact();
        //校验
        String pwd = jrCipher;

        //user是存在的
        if (!jobRecruiter.getJrCipher().equals(pwd)) {
            return new Result(500, "密码错误");
        }
        //服务器创建一个token
        String token = JWTUtils.geneJsonWebToken(jobRecruiter);
//        return new Result(200, "登录成功", token);
        Integer userId= jobRecruiter.getId();
        vop.set("jr_"+userId,token);
        redisTemplate.expire("jr_"+userId,1, TimeUnit.HOURS);
        return new Result(200, "登录成功", userId,token);
    }

//    @GetMapping("checkJrAccount/{jrAccount}")
//    public Result checkAccount(@PathVariable("jrAccount") String jrAccount){
//
//        JobRecruiter jobRecruiter = recruiterService.login(jrAccount);
//
//        if (jobRecruiter!=null){
//            return new Result(500,"用户名已存在");
//        }else {
//            return new Result(200,"用户名可用");
//        }
//    }



    @PostMapping("findByJrId/{jrId}")
    public Result findByjrId(@PathVariable("jrId")Integer jrId){
        JobRecruiter jobRecruiter = recruiterService.findById(jrId);
        return new Result(200,"查询成功",jobRecruiter);
    }

}
